/**
 * 自动创建的对应js目录
 * 创建时间：2018-05-21 16:35:13
 * 技术支持：曹传喜
 * 邮箱：15075107120@139.com
 */
$(function () {
    $.util.exec(function () {
        var userNameEl = $('#l_username').focus();
        try {
            var COOKIE_NAME = 'l_username';
            var _username = $.cookie(COOKIE_NAME);
            if (_username && _username.replace('null', '')) {
                userNameEl.val(_username);
                $('#l_remember').attr('checked', true);
                $('#l_password').focus();
            } else {
                userNameEl.focus();
            }
        } catch (e) {
            userNameEl.focus();
        }

        $('#l_password').bind('keyup', function (event) {
            if (event.keyCode == "13") {
                doLogin();
            }
        });

        $('#l_btn').click(function () {
            doLogin();
        });

        function doLogin() {
            if ($(".msg-error").css("display") == "block") {
                clearTimeout(timer);
                $(".msg-error").css("display", "none");
            }
            if ($("#l_password").val() == "") {
                $("#msg-content").html("请输入密码");
                $(".msg-error").css("display", "block");
                timer = setTimeout(function () {
                    $(".msg-error").css("display", "none");
                }, 3000);
                return;
            }
            $('#l_btn').find('.l-btn-text').text("正在登录...");
            var username = $.trim($('#l_username').val());
            var password = $.trim($('#l_password').val());
            $.page.doLogin(username, password, function (res) {
                var status = parseInt(res.statusCode);
                switch (status) {
                    case 200:
                        // 登录成功才执行记住用户名
                        if ($('#l_remember').prop('checked')) {
                            $.cookie(COOKIE_NAME, username, {path: '/', expires: 15}); // 存储cookie 15天
                        } else {
                            $.cookie(COOKIE_NAME, null, {path: '/'});
                        }
                        window.location = $.util.getRootPath() + '/index.html';
                        break;
                    case 302:
                        $('#l_btn').find('.l-btn-text').text("登 录");
                        $(".msg-content").html(res.message);
                        $(".msg-error").css("display", "block");
                        timer = setTimeout(function () {
                            $(".msg-error").css("display", "none");
                        }, 3000);
                        break;
                    case 500:
                        $('#l_btn').find('.l-btn-text').text("登 录");
                        $(".msg-content").html(res.message);
                        $(".msg-error").css("display", "block");
                        timer = setTimeout(function () {
                            $(".msg-error").css("display", "none");
                        }, 3000);
                        break;
                }
            });
        }
    });
});

setTimeout(function () {
    document.getElementById("bgdiv").style.display = "none";
}, 1000);